#include "editwidget.h"

EditWidget::EditWidget(QWidget *parent) : QDialog(parent)
{
    this->setWindowFlags(Qt::FramelessWindowHint);  //去掉标题栏
    mainLayout = new QVBoxLayout(this);

    modeLayout = new QHBoxLayout();
    countDownMode = new QRadioButton("倒计时模式");
    timingMode = new QRadioButton("定时模式");
    QButtonGroup *modeGroup = new QButtonGroup();
    modeGroup->addButton(countDownMode);
    modeGroup->addButton(timingMode);
    connect(modeGroup,SIGNAL(buttonClicked(int)),this,SLOT(slot_onClickedModeGroup(int)));
    countDownMode->setChecked(true); //设置倒计时模式选中
    modeLayout->addWidget(countDownMode);
    modeLayout->addWidget(timingMode);

    editLayout = new QHBoxLayout();

//    timeEdit->setDisplayFormat("HHSS");

    hourLine = new QLineEdit();
    hourLine->setText("01");
    minLine = new QLineEdit();
    minLine->setText("25");
    QLabel *midLabel1 = new QLabel(":");
    QLabel *midLabel2 = new QLabel(":");
    secLine = new QLineEdit();
    secLine->setText("00");

    editLayout->addWidget(hourLine);
    editLayout->addWidget(midLabel1);
    editLayout->addWidget(minLine);
    editLayout->addWidget(midLabel2);
    editLayout->addWidget(secLine);

    controlLayout = new QHBoxLayout();

    cancelButton = new QPushButton("取消");
    connect(cancelButton,&QPushButton::clicked,this,&EditWidget::slot_onClickedCancelButton);
    OkButton = new QPushButton("确认");
    connect(OkButton,&QPushButton::clicked,this,&EditWidget::slot_onClickedOkButton);
    OkButton->setObjectName("OkButton");
    controlLayout->addWidget(OkButton);
    controlLayout->addWidget(cancelButton);

    mainLayout->addLayout(modeLayout);
    mainLayout->addLayout(editLayout);
    mainLayout->addLayout(controlLayout);

    //加载样式文件
    QFile qssFile(":/new/prefix1/editWidget.qss");
    qssFile.open(QFile::ReadOnly);
    if(qssFile.isOpen())
    {
        this->setStyleSheet(qssFile.readAll());
        qssFile.close();
    }
}

void EditWidget::slot_onClickedOkButton()
{
    int tempHour = hourLine->text().toInt();
    int tempMin = minLine->text().toInt();
    int tempSec = secLine->text().toInt();
    if(hourLine->text().isEmpty()||minLine->text().isEmpty()||secLine->text().isEmpty())
    {
        QMessageBox::warning(nullptr,"提示","您的输入有误！",QMessageBox::Ok);
        return ;
    }
    if(tempMin > 60||tempSec > 60||tempHour > 24)
    {
        QMessageBox::warning(nullptr,"提示","您输入的值超过上限！",QMessageBox::Ok);
        return ;
    }
    if(!isCountDownMode)
    {
        //获取系统时间
        QTime currentTime = QTime::currentTime();
//        QTime tempTime = QTime::fromString(QString("%1:%2:%3")
//                                           .arg(tempHour,2,10,QLatin1Char('0'))
//                                           .arg(tempMin,2,10,QLatin1Char('0'))
//                                           .arg(tempSec,2,10,QLatin1Char('0')));

        int currentTimeBySec = currentTime.hour()*3600 + currentTime.minute()*60 + currentTime.second();
        currentTimeBySec = tempHour*3600 + tempMin*60 + tempSec - currentTimeBySec;
        tempHour =(currentTimeBySec / 3600) % 60;
        tempMin = (currentTimeBySec / 60) % 60;
        tempSec = currentTimeBySec % 60;
    }
    emit signal_changeTime(tempHour,tempMin,tempSec);
    this->close();
}

void EditWidget::slot_onClickedCancelButton()
{
    this->close();
}

void EditWidget::slot_onClickedModeGroup(int buttonID)
{
    if(buttonID == -2)
    {
        isCountDownMode = true;
    }
    if(buttonID == -3)
    {
        isCountDownMode = false;
        QTime currentTime = QTime::currentTime();
        hourLine->setText(QString("%1").arg(currentTime.hour()));
        minLine->setText(QString("%1").arg(currentTime.minute()));
        secLine->setText(QString("%1").arg(currentTime.second()));
    }
}
